# -*- coding: utf-8 -*-
import scrapy,sys,os,json,time
from pymongo import MongoClient
reload(sys).setdefaultencoding('UTF-8')
import logging
from pyquery import PyQuery as pq
import string
import random
import requests

class pwdSpider(scrapy.Spider):
        name = "pwd"
        allowed_domains = ["tmkoo.com"]
        
        def __init__(self):
            self.apiKeyView = 'TEST_APISTORE'
            self.apiPasswordView = '0HTCv5hvPH'
            self.apiKey = 'app18:59:36:d1'
            self.apiPassword = 'w3IarsdObP'
            self.result = []
            print 'self is called!'

        def getApiKeyPassword(self):
            mobile = ''.join([random.choice(string.digits) for _ in range(11)])
            url = 'http://api.tmkoo.com/app-reg.php?mobile=%s' %(mobile)
            res = requests.get(url)
            res.encode = 'utf-8'
            if res.status_code == 200 :
                info = json.loads(res.text)
                self.apiKey = info['apiKey']
                self.apiPassword = info['apiPassword'];
                print "Pwd generate successfully"
                return info
            else :
                print 'pwd generate fiald'

        def store(self,filename,data):
            with open(filename, 'w') as f:  
                json.dump(data,f)

        def closed(self,reason):
            self.store('pwd',self.result)
            

        def start_requests(self):#19000000
            for x in xrange(1,100000):
                mobile = ''.join([random.choice(string.digits) for _ in range(11)])
                url = 'http://api.tmkoo.com/app-reg.php?mobile=%s' %(mobile)
                request = scrapy.Request(url,callback=self.parse)
                request.meta['item'] = x
                yield request

        def parse(self,response):
            if response.status == 200:
                item = response.meta['item']
                info = json.loads(response.body)
                self.result.append({info['apiKey']:info['apiPassword']})
                print "Pwd generate successfully"
                if len(self.result)>10000:
                    self.store('pwd'+str(item)+'.json',self.result)
                    print "stored successfully"
                    del self.result[:]
